<?php

namespace App\Http\Controllers\Admin;

use App\Exceptions\CourseExistsException;
use App\Models\Course;
use App\Models\Series;

class CourseController extends Controller
{
    /*
     * @api get /course-api/a/courses 课程列表
     * @version 0.0.1
     * @group admin
     *
     * @response 200 正常结果 {
     *  "code":0,
     *  "msg":"",
     *  "data":{
     *    "items":[]
     * }
     * }
     */
    public function index()
    {
        $courses = Course::orderBy('id', 'desc')
            ->get();
        $items = [];
        /** @var Course $courseObj */
        foreach ($courses as $courseObj) {
            $items[] = [
                'id' => $courseObj->id,
                'name' => $courseObj->getName(),
                'description' => $courseObj->getDescription(),
                'level' => $courseObj->getLevel(),
                'isPublished' => $courseObj->getPublished(),
                'cover' => $courseObj->cover()->first(),
                'price' => $courseObj->getPrice(),
                'discountPrice' => $courseObj->getDiscountPrice(),
                'lessonCount' => $courseObj->getLessonCount()
            ];
        }
        return $this->sendSuccessJson(['items'=>$items]);
    }

    /*
     * @api post /course-api/a/courses 新增课程
     * @version 0.0.1
     * @group admin
     *
     * @param body string name 课程名称
     * @param body string description 课程描述
     * @param body int level 课程等级
     *
     * @response 200 正常结果 {
     *  "code":0,
     *  "msg":"",
     *  "data":{}
     * }
     */
    public function store()
    {
        $this->validate($this->request, [
            'name' => 'required|string',
            'description' => 'required|string',
            'level' => 'required|integer|max:12|min:0',
            'cover' => 'required|integer',
            'lessonCount' => 'required|integer',
            'price' => 'required',
            'discountPrice' => 'required'
        ]);
        $name = $this->request->input('name');
        $description = $this->request->input('description');
        $level = $this->request->input('level');
        $cover = $this->request->input('cover');
        $lessonCount = $this->request->input('lessonCount');
        $price = $this->request->input('price');
        $discountPrice = $this->request->input('discountPrice');

        if (Course::where('level', $level)->exists()) {
            throw new CourseExistsException();
        }
        $courseObj = new Course();
        $courseObj->setName($name);
        $courseObj->setDescription($description);
        $courseObj->setLevel($level);
        $courseObj->setCover($cover);
        $courseObj->setLessonCount($lessonCount);
        $courseObj->setPrice($price);
        $courseObj->setDiscountPrice($discountPrice);
        $courseObj->save();
        return $this->sendSuccessJson($courseObj->getId());
    }

    /*
     * @api put /course-api/a/courses/{courseId} 更新课程
     * @version 0.0.1
     * @group admin
     *
     * @param body int courseId 学期ID
     * @param body string name 课程名称
     * @param body string description 课程描述
     *
     * @response 200 正常结果 {
     *  "code":0,
     *  "msg":"",
     *  "data":{}
     * }
     */
    public function update($courseId)
    {
        $this->request->merge(['courseId' => $courseId]);
        $this->validate($this->request, [
            'courseId' => 'required|integer',
            'name' => 'required|string',
            'description' => 'required|string',
            'cover' => 'required|integer',
            'lessonCount' => 'required|integer',
            'price' => 'required',
            'discountPrice' => 'required'
        ]);
        $name = $this->request->input('name');
        $description = $this->request->input('description');
        $cover = $this->request->input('cover');
        $lessonCount = $this->request->input('lessonCount');
        $price = $this->request->input('price');
        $discountPrice = $this->request->input('discountPrice');
        /** @var Course $courseObj */
        $courseObj = Course::find($courseId);
        $courseObj->setName($name);
        $courseObj->setDescription($description);
        $courseObj->setCover($cover);
        $courseObj->setLessonCount($lessonCount);
        $courseObj->setPrice($price);
        $courseObj->setDiscountPrice($discountPrice);
        $courseObj->save();
        return $this->sendSuccessJson();
    }

    /*
     * @api put /course-api/a/courses/level 更新课程级别
     * @version 0.0.1
     * @group admin
     *
     * @param body array courseIds 课程ID
     *
     * @response 200 正常结果 {
     *  "code":0,
     *  "msg":"",
     *  "data":{}
     * }
     */
    public function updateLevel()
    {
        $this->validate($this->request, [
            'courseIds' => 'required|array',
        ]);
        $courseIds = $this->request->input('courseIds');
        foreach ($courseIds as $index => $courseId) {
            /** @var Course $courseObj */
            $courseObj = Course::find($courseId);
            $courseObj->setLevle($index);
            $courseObj->save();
        }
        return $this->sendSuccessJson();
    }

    /*
     * @api put /course-api/a/courses/{courseId}/publish 发布课程
     * @version 0.0.1
     * @group admin
     *
     * @param body int courseId 课程ID
     * @param body int isPublished 是否发布，0，1
     *
     * @response 200 正常结果 {
     *  "code":0,
     *  "msg":"",
     *  "data":{}
     * }
     */
    public function publish($courseId)
    {
        $this->request->merge(['courseId' => $courseId]);
        $this->validate($this->request, [
            'courseId' => 'required|integer',
            'isPublished' => 'required|integer'
        ]);
        $isPublished = $this->request->input('isPublished');
        /** @var Course $courseObj */
        $courseObj = Course::find($courseId);
        $courseObj->setPublished($isPublished);
        $courseObj->save();
        return $this->sendSuccessJson();
    }

    /*
     * @api delete /course-api/a/courses/{courseId} 删除课程
     * @version 0.0.1
     * @group admin
     *
     * @param body int courseId 课程ID
     *
     * @response 200 正常结果 {
     *  "code":0,
     *  "msg":"",
     *  "data":{
     *    "items":[]
     * }
     * }
     */
    public function destroy($courseId)
    {
        /** @var Course $courseObj */
        $courseObj = Course::find($courseId);
        if ($courseObj->getPublished()) {
            // @todo
        }

        Course::destroy($courseId);
        return $this->sendSuccessJson();
    }
}
